import instance from "../url/lifedata.js";
import { success, failure,warning } from "../static/js/alert.js";

const get_life_data = async (user_id, Type, select) => { 
    
    if (!select) { 
        select=0
    }
    return await instance.get(`/blog/life_data/get_data?user_id=${user_id}&type=${Type}&select=${select}`)
        .then((response) => { 
            // console.log(response)
            if (response == 0) { '服务器超时' }
            else if (response == 404) {
                warning("数据查询失败!")
            } else {
                if (response.length == 0) { 
                    return []
                }
                return response
            }
        });
}
const get_daily_data = async (user_id) => { 
    return await instance.get(`/blog/life_data/get_data/life?user_id=${user_id}`)
        .then((response) => { 
            return response
    });
}

const get_food_data = async (user_id) => { 
    return await instance.get(`/blog/life_data/get_data/food?user_id=${user_id}`)
        .then((response) => { 
            return response
    });
}
const get_diary_data = async (user_id) => { 
    return await instance.get(`/blog/life_data/get_data/diary?user_id=${user_id}`)
        .then((response) => { 
            return response
    });
}
const get_default_data = async (user_id) => {
    return await instance.get(`/blog/life_data/get_data/default?user_id=${user_id}`)
        .then((response) => {
            if (response) { 
                return response
            }
    });
}

const fix_life_data = async (user_id, id, kind) => {
    // 未知传输过来还是string
    if (typeof (id) === 'string') {
        return await instance.post(`/blog/life_data/fix_data?user_id=${user_id}&id=${id}&kind=${kind}`)
            .then((response) => {
                if (response == 1) {
                    success()
                    return 1
                } else {
                    failure()
                    return 0
                }
            });
    }
    else {
        // 后端问题。将数据投入数组中。
        let id_list = []
        for (let i = 0; i < id.length; i++) {
            id_list.push(id[i])
        }
        return await instance.post(`/blog/life_data/fix_data?user_id=${user_id}&kind=${kind}`,
        id_list
        ).then((response) => {
        if (response == 1) { 
                success()
        } else { 
                failure()
            }
        });
    }
   

}
const up_mylife_data = async (formdata) => { 
    return  await instance.post(`/blog/life_data/`, formdata,
        {
            headers:
                { 'Content-Type': 'multipart/form-data' }
        }
    ).then((response) => {
        if (response == 1) {
                success()
        } else { 
                failure()
        }
        //由于处理了：re是response.data
    });
}
const get_mylife_data_Byid = async (user_id, id) => {
   
 return await instance.get(`/blog/life_data/get_data/byID?user_id=${user_id}&id=${id}`)
        .then((response) => {
            if (response !== 0) {
                return response
            } else { 
                warning("状态错误!")
                return 0
            }
    });
 }



export {
    get_life_data, get_daily_data, get_food_data, get_diary_data, up_mylife_data, get_default_data, fix_life_data
    ,get_mylife_data_Byid
}




